package com.ajview.core.security.handler;

import com.ajview.common.core.domain.enums.ResultCodeEnum;
import com.ajview.common.core.domain.vo.ResultVo;
import com.ajview.common.utils.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * 身份认证 入口
 * 认证失败处理类 返回未授权
 *
 * @author zhangjunjie
 */
@Component
@Slf4j
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable {

    private static final long serialVersionUID = 1668430673415174L;

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        log.debug("请求访问：{}，认证失败，无法访问系统资源,异常信息为{}", request.getRequestURI(), authException.getMessage());
        ServletUtils.renderString(response, ResultVo.result(ResultCodeEnum.UNAUTHORIZED).toString());
    }
}
